package com.zzyl.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.CustomerReservationDto;
import com.zzyl.dto.CustomerReservationPageParamDto;
import com.zzyl.entity.CustomerReservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.vo.CustomerReservationVo;
import com.zzyl.vo.ReservationTimeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {

    @Autowired
    private CustomerReservationMapper customerReservationMapper;

    //查询取消预约次数
    @Override
    public Long getCancellCount(Long userId) {
        LocalDateTime start = LocalDateTimeUtil.beginOfDay(LocalDateTime.now());
        LocalDateTime end = LocalDateTimeUtil.endOfDay(LocalDateTime.now());
        Long count = customerReservationMapper.selectCancelCount(userId,start,end);
        return count;
    }

    //查询时间段剩余次数
    @Override
    public List<ReservationTimeVo> getTimeCount(Long time) {
        //将时间戳转换为LocalDateTime
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        //获取当天开始和结束时间
        LocalDateTime start = LocalDateTimeUtil.beginOfDay(localDateTime);
        LocalDateTime end = LocalDateTimeUtil.endOfDay(localDateTime);
        //查询
        List<ReservationTimeVo> result = customerReservationMapper.selectTimeCount(start,end);
        return result;
    }

    //新增预约
    @Override
    public void add(CustomerReservationDto customerReservationDto, Long userId) {
        //更换映射
        CustomerReservation customerReservation = new CustomerReservation();
        BeanUtils.copyProperties(customerReservationDto, customerReservation);

        /* 【同一手机号】同一时间段不可重复预约 */
        //将传来的预约时间 String --> LocalDateTime
        String insertTime = customerReservation.getTime();
        LocalDateTime time = DateUtil.parseLocalDateTime(insertTime, "yyyy-MM-dd HH:mm:ss");
        //查询该用户在该时间段是否已有预约
        Long count = customerReservationMapper.selectByTime(customerReservation.getMobile(),time);
        if (count > 0) {
            throw new BaseException(BasicEnum.RESERVATION_INSERT_FAIL_TIME_REPEAT);
        }

        //新增预约
        //customerReservation.setCreateBy(userId);
        customerReservation.setStatus(0);
        customerReservationMapper.insertReservation(customerReservation);
    }

    //分页查询
    @Override
    public PageResponse<CustomerReservationVo> getPage(Integer pageNum,Integer pageSize,Integer status,Long userId) {
        PageHelper.startPage(pageNum, pageSize);
        Page<CustomerReservationVo> result = customerReservationMapper.selectByPage(status,userId);
        return PageResponse.of(result,CustomerReservationVo.class);
    }

    //取消预约
    @Override
    public void changeStatusCancel(Long id) {
        //只有状态为 0-带上门 能进行取消操作
        Integer status = customerReservationMapper.selectStatus(id);
        if (status != 0){
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_FAIL_STATUS);
        }
        //取消预约
        customerReservationMapper.updateStatus(id);
    }


}
